package com.starrin.trashtalk;

import com.starrin.trashtalk.dao.PlayerDAO;
import com.starrin.trashtalk.dao.PlayerDAOImpl;
import com.starrin.trashtalk.model.Player;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

/**
 * Created by larsstarrin on 17/10/14.
 */

@ManagedBean
@RequestScoped
public class PlayerConverter implements Converter {

    private PlayerDAO playerDAO = new PlayerDAOImpl();

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) {
        return playerDAO.findById(Long.valueOf(s));
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
        Player player = (Player) o;
        return String.valueOf(player.getId());
    }
}
